home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / gc-nov90.lha / gc-26nov90 / gctest.C < prev    next >
C/C++ Source or Header  |  1990-11-01  |  3KB  |  196 lines

  1. /* Test program for the gcalloc.c */
  2.  
  3. /* Externals */
  4.  
  5. #include <stdio.h>
  6. #include "gcalloc.h"
  7.  
  8. struct  cell  {
  9.     cell  *car;
  10.     cell  *cdr;
  11.     int  value;
  12.     cell( cell *initcar, cell *initcdr, int initvalue );
  13.     GCCLASS( cell );
  14. };
  15.  
  16. typedef  cell* CP;
  17.  
  18. void  cell::GCPointers( )  {
  19.     gcpointer( car );
  20.     gcpointer( cdr );
  21. }
  22.  
  23. cell::cell( cell *initcar, cell *initcdr, int initvalue )
  24. {
  25.     GCALLOC( cell );
  26.     car = initcar;
  27.     cdr = initcdr;
  28.     value = initvalue;
  29. }
  30.  
  31. struct vector {
  32.    vector  *car;
  33.    vector  *cdr;
  34.    int  value1;
  35.    char bytes[1000];
  36.    int  value2;
  37.    vector( vector* x, vector* y, int v1, int v2 );
  38.    GCCLASS( vector );
  39. };
  40.  
  41. typedef  vector* VP;
  42.  
  43. void  vector::GCPointers( )  {
  44.     gcpointer( car );
  45.     gcpointer( cdr );
  46. }
  47.  
  48. vector::vector( vector* x, vector* y, int v1, int v2 )  {
  49.     GCALLOC( vector );
  50.     car = x;
  51.     cdr = y;
  52.     value1 = v1;
  53.     value2 = v2;
  54. }
  55.     
  56. /* Test program */
  57.  
  58. int  init_global = 2,
  59.      array_global[ 1000 ];
  60.  
  61. void  printtree( CP zp )
  62. {
  63.     CP  tp;
  64.  
  65.     tp = zp;
  66.     while  (tp != NULL)  {
  67.        printf( "%x: %d  ", tp, tp->value );
  68.        tp = tp->cdr;
  69.     }
  70.     printf( "\n" );
  71.     tp = zp;
  72.     while  (tp != NULL)  {
  73.        printf( "%x: %d  ", tp, tp->value );
  74.        tp = tp->car;
  75.     }
  76.     printf( "\n" );
  77. }
  78.  
  79. void  listtest1()
  80. {
  81.     int  i, j;
  82.     CP  lp, zp;
  83.     
  84.     printf( "List test 1\n" );
  85.     lp = NULL;
  86.     for (i = 0; i <= 1000 ; i++)  {
  87.        if  (i % 15 != 14)
  88.           printf( "%d ", i );
  89.        else
  90.           printf( "%d\n", i );
  91.        zp = new cell( NULL, lp, i );
  92.        lp = zp;
  93.        gccollect();
  94.        zp = lp;
  95.        for (j = i; j >= 0 ; j--)  {
  96.           if ((zp == NULL) || (zp->value != j ))
  97.              printf( "LP is not a good list when j = %d\n", j );
  98.           zp = zp->cdr;
  99.        }
  100.     }
  101.     printf( "\n" );           
  102. }
  103.  
  104. void  vectortest()
  105. {
  106.     int  i, j;
  107.     VP  lp, zp;
  108.     
  109.     printf( "Vector test\n" );
  110.     lp = NULL;
  111.     for (i = 0; i <= 100 ; i++)  {
  112.        if  (i % 15 != 14)
  113.           printf( "%d ", i );
  114.        else
  115.           printf( "%d\n", i );
  116.        zp = new vector( NULL, lp, i, i );
  117.        lp = zp;
  118.        gccollect();
  119.        zp = lp;
  120.        for (j = i; j >= 0 ; j--)  {
  121.           if ((zp == NULL) || (zp->value1 != j )  ||  (zp->value2 != j))
  122.              printf( "LP is not a good list when j = %d\n", j );
  123.           zp = zp->cdr;
  124.        }
  125.     }
  126.     printf( "\n" );           
  127. }
  128.  
  129. CP  treetest()
  130. {
  131.     int  i;
  132.     CP  tp, zp;
  133.  
  134.     printf( "Tree test\n" );
  135.     tp = new cell( NULL, NULL, 0 );
  136.     for (i = 1; i <= 5; i++)  {
  137.        zp = new cell( tp, tp, i );
  138.        tp = zp;
  139.     }
  140.     gccollect();
  141.     zp = new cell( tp, tp, 6 );
  142.     gccollect();
  143.     printtree( zp );
  144.     return( zp );
  145. }
  146.  
  147. void  listtest2()
  148. {
  149.     int  i, j, length = 10000, repeat = 1000;
  150.     CP  lp, zp;
  151.  
  152.     printf( "List Test 2\n" );
  153.     for (i = 0; i < repeat; i++)  {
  154.        if  (i % 15 != 14)
  155.           printf( "%d ", i );
  156.        else
  157.           printf( "%d\n", i );
  158.        /* Build the list */
  159.        lp = NULL;
  160.        for  (j = 0; j < length; j++)  {
  161.           zp = new cell( NULL, lp, j );
  162.           lp = zp;
  163.        }
  164.        /* Check the list */
  165.        zp = lp;
  166.        for (j = length-1; j >= 0 ; j--)  {
  167.           if ((zp == NULL) || (zp->value != j ))
  168.              printf( "LP is not a good list when j = %d\n", j );
  169.           zp = zp->cdr;
  170.        }
  171.     }
  172.     printf( "\n" );           
  173. }
  174.  
  175. CP  gp;        /* A global pointer */
  176.  
  177. main( int argc, char *argv[] )
  178. {
  179.     /* List construction test */
  180.     listtest1();
  181.  
  182.     /* List of vectors > 1 page */
  183.     vectortest();
  184.  
  185.     /* Tree construction test */
  186.     gp = treetest();
  187.  
  188.     /* 1000 10000 node lists */
  189.     listtest2();
  190.  
  191.     /* Check that tree is still there */
  192.     printtree( gp );
  193.  
  194.     exit( 0 );
  195. }
  196.